#!/bin/bash
BASE_HOME=`/usr/bin/dirname "$0"`
FILE_NAME=`basename "$0"`
PROCESS_ID="$$"
CONF_FILE="${BASE_HOME}/mysql_config.list"
##load mysql config file mysql_config.list
while read line; do
    eval $line
done < ${CONF_FILE}


#create logfile path if not exists
LOGFILE_PATH=`echo ${LOGFILE%/*}`
if [ ! -d ${LOGFILE_PATH} ]; then
                mkdir -p  ${LOGFILE_PATH} 
fi

##record scripts running logs
exec >> ${LOGFILE} 2>&1

echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]keepalived into master status,if remote host is down and set local host as master role"
##if this scripts is running and exit
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]check scripts ps -ef | grep \"$0\" | grep -v ${PROCESS_ID} | grep -v grep is running "
check_running=`ps -ef | grep "$0" | grep -v ${PROCESS_ID} | grep -v grep |wc -l`
ps -ef | grep "$0" | grep -v ${PROCESS_ID} | grep -v grep
if [ ${check_running} -ge 1 ]; then
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]scripts $0 is running ${check_running} and exit"
                #exit 0
else
    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]scripts $0 is not running ${check_running} and continue to switch"
fi 

# mysql CMD
MYSQL_L_CMD="${MYSQL_CMD} -u${MYSQL_USER} -p${MYSQL_PASS} -P${MYSQL_PORT} -h${LOCAL_HOST} --protocol=tcp"
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]MYSQL_L_CMD is \"${MYSQL_L_CMD}\" "

##mysql is slave and sync check sql_thread delay from io_thread
#create binlog parse to sql work dir if not exists
if [ ! -d ${sqlpath} ]; then
                mkdir -p  ${sqlpath} 
fi

get_slave_delay_status(){

# slave status
${MYSQL_L_CMD} -e "show slave status \G" > ${sqlpath}/tmp_local_slave.txt
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]show slave status \G for local mysql "
cat ${sqlpath}/tmp_local_slave.txt

# Master_Log_File
READFILE=`cat ${sqlpath}/tmp_local_slave.txt |grep -w "Master_Log_File"|grep -v "Relay"|awk -F": " '{print $2}'`

#Relay_Master_Log_File
EXECFILE=`cat ${sqlpath}/tmp_local_slave.txt |grep -w "Relay_Master_Log_File"|grep "Relay"|awk -F": " '{print $2}'`

# Exec_Master_Log_Pos
EXECPOS=`cat ${sqlpath}/tmp_local_slave.txt | grep -w 'Exec_Master_Log_Pos' | awk -F ': ' '{print $2}'`

# Read_Master_Log_Pos
READPOS=`cat ${sqlpath}/tmp_local_slave.txt | grep -w "Read_Master_Log_Pos" | awk -F ': ' '{print $2}'`

echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]slave_readpos:${READPOS}"
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]slave_execpos:${EXECPOS}"
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]slave_readfile:${READFILE}"
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]slave_execfile:${EXECFILE}"
}



##record the VIP is in local host 
WVIP_COUNT=`ip addr | grep -v grep | grep "${WRITE_VIP}" |wc -l`
if [ ${WVIP_COUNT} -eq 1 ]; then
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]VIP ${WRITE_VIP} on local host ${LOCAL_HOST}"
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]VIP `ip addr | grep -v grep | grep \"${WRITE_VIP}\"`"
else
    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]VIP ${WRITE_VIP} not exists local host ${LOCAL_HOST}"
fi


##check remote host is down by ping 
remote_host_ping_alive()
{
                result_ping=0
                ping -c ${PINGDOWN_C} ${REMOTE_HOST} 
                if [ $? -eq 0 ]; then
                                result_ping=1
                else
                    result_ping=0
                fi
}

echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]start ping remote host by \"ping -c ${PINGDOWN_C} ${REMOTE_HOST}\" "
i=1
down_time=0
while [ ${i} -le ${PINGDOWN_TOTAL} ]
do
                remote_host_ping_alive
                if [ ${result_ping} -eq 1 ]; then
                                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]ping remote host ${REMOTE_HOST} is alived by ${i} times and exit"
                                down_time=1
                                break;
                else
                    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]ping remote host ${REMOTE_HOST} is down by ${i} times"
                    i=$(($i+1))
                    down_time=0
                fi
done
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]end ping remote host by \"ping -c ${PINGDOWN_C} ${REMOTE_HOST}\" "


if [ ${down_time} -eq 1 ]; then
        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]ping remote host ${REMOTE_HOST} is alived and not any operation for local mysql database"
else
        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]ping remote host ${REMOTE_HOST} is down and set local mysql database for master role"
    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]check local host vip status "
    ps -ef | grep -i keepalived | grep -v grep
    ip addr
        get_slave_delay_status
        ##if slave sql_thread is not start and started
        SQL_STATUS="`grep -w  \"Slave_SQL_Running\" ${sqlpath}/tmp_local_slave.txt|awk -F\": \" '{print $2}'`"
    if [ "${SQL_STATUS}"x == "Yes"x ]; then
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]Slave_SQL_thread running status :${SQL_STATUS}"
        elif [ "${SQL_STATUS}"x != "Yes"x ] && [ "${SQL_STATUS}"x != ""x ]; then
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]Slave_SQL_thread running status :${SQL_STATUS} and will start sql_thread"  
                ${MYSQL_L_CMD} -e "start slave sql_thread;"
                if [ $? -eq 0 ]; then
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]slave_SQL_thread  started sucusessfull and show slave status\G"
                ${MYSQL_L_CMD} -e "show slave status \G;"
                else
                        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]slave_SQL_thread  started failed and exit the mysql switch\G"
                        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]keepalived end master status and exit scripts running"
                        exit 1
                fi    
        fi
        if [ "${SQL_STATUS}"x != ""x ]; then
                while true
                do
                        if [ ${READFILE} == ${EXECFILE} ] && [ ${READPOS} == ${EXECPOS} ];then
                                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]slave SQL_thread catched up io_thread and continue"
                                break
                        else
                                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]slave SQL_thread not catched up io_thread and sleep 5 seconds"
                                sleep 5
                        fi
                        get_slave_delay_status
                done
        else
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]local mysql is not configure slave policy and continue"
        fi

    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]set local mysql database for master role start"
    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]show master status,set global super_read_only = OFF;set global read_only = OFF,stop slave, reset slave all"
    ${MYSQL_L_CMD} -e "show master status \G"
    ${MYSQL_L_CMD} -N -L -s -e "set global super_read_only = OFF;set global read_only = OFF;show variables like 'read_only'; show variables like 'super_read_only';"
    ${MYSQL_L_CMD} -e "stop slave; reset slave all;"
        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]set local mysql database for master role completed"
fi 

##record the VIP is in local host 
WVIP_COUNT=`ip addr | grep -v grep | grep "${WRITE_VIP}" |wc -l`
if [ ${WVIP_COUNT} -eq 1 ]; then
        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]VIP ${WRITE_VIP} on local host ${LOCAL_HOST}"
        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]VIP `ip addr | grep -v grep | grep \"${WRITE_VIP}\"`"
else
    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]VIP ${WRITE_VIP} not exists local host ${LOCAL_HOST}"
fi
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]keepalived end master status and exit scripts running"
